
new Vue({

    data:{
        //视频类别
        types:[],
        currentTypeId : 0,
        videos: [],
        currentTag:'all',
        paginate:{},
        loading:false,
        lang: document.getElementsByTagName('html')[0].lang,
        tagMap:{
            'all': 0,
            'company':1,
            'product':2,
            'case':3,
            'technology':4,
        },
    },

    filters:{
        textOverflow: function(value, length){
            if( document.getElementsByTagName('html')[0].lang === 'zh' ){
                length = 20;
            }else{
                length = 90;
            }
            if(value && value.length > length ){
                return value.substring(0,length) + '...';
            }else{
                return value;
            }
        }
    },

    methods:{

        getArticleTitle: function (video) {
            if (document.getElementsByTagName('html')[0].lang === 'zh') {
                return video.name;
            }else{
                return video.english_title;
            }
        },

        getVideoList:function(page){
            var self = this;
            self.loading=true;
            page = page || 1;
            var p =  self.$http.get('/api/web-page/video?video_type_id='+self.currentTypeId+'&per_page=9&page='+page).then(function(response){
                return response.json();
            }).catch(function(error){
                throw error;
            });

            p.then(function(result){
                if( result.code !== 0 ){
                    throw new Error( "请求视频列表失败，错误号："+result.code );
                }
                self.videos = result['videos_paginate'].data;
                self.paginate = result['videos_paginate'];
                self.loading=false;
            })
        },

        /**
         *  切换分类
         *  @param {string} tagName
         */
        changeType:function(tagName){
            this.currentTypeId = this.tagMap[tagName];
            location.hash = tagName;
            this.currentTag = tagName;
            this.videos = [];
            this.getVideoList();
        },

        changePage:function(number){
            this.videos = [];
            this.getVideoList(number)
        },

        /**
         *  解析当前的分类
         *  @return {undefined}
         */
        getCurrentTag () {
            var currentTag = location.hash || 'all';
            this.currentTag = currentTag.replace('#','');
            this.currentTypeId = this.tagMap[currentTag];
        }

    },

    mounted:function(){
        this.getCurrentTag();
        //请求分类
        //this.getTypes();
        //请求列表数据
        this.getVideoList();

    },

    components:{
        bymPagination:bymPagination
    }

}).$mount("#video");


